/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2016
 */

package com.jzwl.site.manager.contentv2.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jzwl.common.constant.Constants;
import com.jzwl.common.id.Sequence;
import com.jzwl.common.page.PageObject;
import com.jzwl.system.base.dao.BaseDAO;



@Repository("siteContentV2Dao")
public class SiteContentV2Dao {
	
	@Autowired
	private BaseDAO baseDAO;//dao基类，操作数据库
	
	@Autowired
	private Constants constants;
	
	public boolean addSiteContentV2(Map<String, Object> map) {
		
		//自动注入时间戳为ID 酌情修改数据库类型为bigint  int会越界
		map.put("contenId",  Sequence.nextId());
		map.put("createTime", new Date());
		String sql = "insert into " + constants.getCurrentDB() + "site_content_v2 " 
				 + " (contenId,columId,title,digest,content,cover,outUrl,createTime) " 
				 + " values "
				 + " (:contenId,:columId,:title,:digest,:content,:cover,:outUrl,:createTime)";
		
		return baseDAO.executeNamedCommand(sql, map);
	}
	
	
	public String getColumns() {
		return ""
				+" contenId as contenId,"
				+" columId as columId,"
				+" title as title,"
				+" digest as digest,"
				+" content as content,"
				+" cover as cover,"
				+" outUrl as outUrl"
				;
	}
	
	public String getColumnse() {
		return ""
				+" contenId as contenId,"
				+" t.columId as columId,"
				+" title as title,"
				+" digest as digest,"
				+" content as content,"
				+" cover as cover,"
				+" outUrl as outUrl"
				;
	}
	

	public PageObject querySiteContentV2List(Map<String,Object> map) {
		// [column]为字符串拼接, {column}为使用占位符. 如username='[username]',偷懒时可以使用字符串拼接 
		// [column] 为PageRequest的属性
		
		String sql="select " + getColumnse() + ",cn.name from " + constants.getCurrentDB() + "site_content_v2 t "
				+ "LEFT JOIN " + constants.getCurrentDB() + "site_column_v2 cn on t.columId=cn.columId where 1=1 ";
		
		  	
		  		if(null !=map.get("columId") && StringUtils.isNotEmpty(map.get("columId").toString())){
				  		sql=sql+ " and t.columId  = " + map.get("columId") +"";
			  	}
		  	
		  		if(null !=map.get("title") && StringUtils.isNotEmpty(map.get("title").toString())){
				  		sql=sql+ " and t.title  like '%" + map.get("title") +"%' ";	
			  	}
		  	
		  		if(null !=map.get("digest") && StringUtils.isNotEmpty(map.get("digest").toString())){
				  		sql=sql+ " and t.digest  = " + map.get("digest") +"";
			  	}
		  	
		  		if(null !=map.get("content") && StringUtils.isNotEmpty(map.get("content").toString())){
				  		sql=sql+ " and t.content  = " + map.get("content") +"";
			  	}
		  	
		  		if(null !=map.get("cover") && StringUtils.isNotEmpty(map.get("cover").toString())){
				  		sql=sql+ " and t.cover  = " + map.get("cover") +"";
			  	}
		  	
		  		if(null !=map.get("outUrl") && StringUtils.isNotEmpty(map.get("outUrl").toString())){
				  		sql=sql+ " and t.outUrl  = " + map.get("outUrl") +"";
			  	}
					
		sql=sql+ " order by contenId ";
		
		PageObject po = baseDAO.queryForMPageList(sql, new Object[]{},map);
		
		return po;
	}

	public boolean updateSiteContentV2(Map<String, Object> map) {
		
		String sql ="update " + constants.getCurrentDB() + "site_content_v2 set ";
				  	if(null !=map.get("columId") && StringUtils.isNotEmpty(map.get("columId").toString())){
					  	sql=sql+ "			columId=:columId ,";
				  	}
				  	if(null !=map.get("title") && StringUtils.isNotEmpty(map.get("title").toString())){
					  	sql=sql+ "			title=:title ,";
				  	}
				  	if(null !=map.get("digest") && StringUtils.isNotEmpty(map.get("digest").toString())){
					  	sql=sql+ "			digest=:digest ,";
				  	}
				  	if(null !=map.get("content") && StringUtils.isNotEmpty(map.get("content").toString())){
					  	sql=sql+ "			content=:content ,";
				  	}
				  	if(null !=map.get("cover") && StringUtils.isNotEmpty(map.get("cover").toString())){
					  	sql=sql+ "			cover=:cover ,";
				  	}
				  	if(null !=map.get("outUrl") && StringUtils.isNotEmpty(map.get("outUrl").toString())){
					  	sql=sql+ "			outUrl=:outUrl ,";
				  	}
				sql = sql.substring(0, sql.length()-1);
				sql = sql + " where contenId=:contenId";
		return  baseDAO.executeNamedCommand(sql, map);
	}

	public boolean deleteSiteContentV2(Map<String, Object> map) {
		
		String sql = "delete from " + constants.getCurrentDB() + "site_content_v2 where contenId in ("+ map.get("contenId")+ " ) ";

		return baseDAO.executeNamedCommand(sql, map);
	}

	public boolean delSiteContentV2(Map<String, Object> map) {
		
		String sql ="update " + constants.getCurrentDB() + "site_content_v2 set "
				+ " isDelete =:"+map.get("isDelete")+" "
				+ " where contenId=:contenId";
		
		return  baseDAO.executeNamedCommand(sql, map);
	}
	
	
	@SuppressWarnings("unchecked")
	public Map<String,Object> getByContenId(Map<String, Object> map) {
		
		String sql = "select " + getColumns() + " from " + constants.getCurrentDB() + "site_content_v2 where contenId = "+ map.get("contenId") + "";
		
		return baseDAO.queryForMap(sql);
	}
	
	public List<Map<String,Object>> querySelectValue(Map<String,Object> map) {
		
		String sql = " SELECT columId,name,NumOrder "
				+ "    from " + constants.getCurrentDB() + "site_column_v2";
	 return	baseDAO.queryForList(sql);
	}
	

}
